<?php
namespace Home\Controller;

class UserController extends CommonController {
     
//     public function index(){
//         if (isset($_SESSION['username'])){           
//                 $this->display();
//             }else{
//             header("location: ". __ROOT__."/index.php/login");
//         }
//     }    
    
    //加载用户信息
    public function find(){ 
        $user = M('user');      

        $id  = $_SESSION['id'];            
    
        $list = $user->find($id);
        $list['password'] = null;
 
        $data['data']=$list;
        $this->ajaxReturn($data);
    }
    
    //更新数据操作
    public function update(){
        $user = D('user');
        $data=I('post.');
        if (!$data['id']){
            $data['id'] = $_SESSION['id'];
        }
        $id = $data['id'];
        $password = $data['password'];
/*         if($data['newPassword']!=$data['rePassword']){
            $result['success']=false;
            $result['msg']='确认密码与新密码不一致';
        }elseif(!($user->where("id=".$id." and password='".$password."'")->find())){
            $result['success']=false;
            $result['msg']='原密码不正确';
        } */
        if (!$user->create()){       
            // 如果创建失败 表示验证没有通过 输出错误提示信息
            $result['success'] = false;
            $result['msg'] = $user->getError();
            exit($this->ajaxReturn($result));
        }elseif(!($user->where("id=".$id." and password='".$password."'")->find())){
            $result['success']=false;
            $result['msg']='原密码不正确';
        }elseif($user->where("id=".$id)->setField('password',$data['newPassword'])!==false){
            $result['success']=true;//返回修改后的状态
			$result['msg']='修改成功';//状态信息
        }else{
            $result['success']=false;
			$result['msg']='修改失败';
        }
        $this->ajaxReturn($result);
    }
}